<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Appeal as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use think\App;

class Appeal extends Base
{
    protected BusObj $busObj;
    protected $indexWhere = [];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function index()
    {
        $classify = $this->request->param('classify', '', 'trim');
        $this->indexField = "*,order_id as get_order";
        if (empty($classify)) {
            $this->indexWhere = [
                ['create_time', 'between', Time::month()]
            ];
        } else {
            $timestamp = explode('~', $classify);
            $this->indexWhere = [
                ['create_time', 'between', [
                    strtotime($timestamp[0]),
                    strtotime($timestamp[1])
                ]]
            ];
        }

        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function success()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        try {
            $order = $this->busObj->setAppealById($id);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::status_normal, "审核通过");

    }

    public function setError()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        $msg = $this->request->param('msg', '', 'trim');
        if (empty($msg)) {
            return Show::json_show(Status::status_error, "请输入拒绝原因");
        }
        try {
            $result = $this->busObj->setAppealOrder($id, $msg);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "操作成功");
        } else {
            return Show::json_show(Status::status_error, "操作失败");
        }
    }
}